//
//  HouseTypeInfoViewController.m
//  YiJuZhangShou2.0
//
//  Created by URANUS on 16/3/25.
//  Copyright © 2016年 Mr.X. All rights reserved.
//

#import "HouseTypeInfoViewController.h"
#import "HouseTypeInfoView.h"
#import "Masonry.h"
#import "CarouselAdvertiseView.h"
#import "HouseTypeInfoBottomBarView.h"
#import "HouseTypeInfoBottomBarModel.h"
#import "MoreHouseTypeView.h"
#import "HousesView.h"
#import "MoreHouseTypeModel.h"
//#import "HouseInfoViewController.h"
#import "HousesModel.h"
#import "HousesViewModel.h"
#import "MoreHouseTypeViewModel.h"
#import "HouseTypeInfoViewModel.h"
#import "HouseTypeInfoCommentViewModel.h"
//#import "HouseTypeCommentController.h"
#import "HouseLoanCalculateViewController.h"
#import "YijuWebViewController.h"
#import "UINavigationItem+offset.h"

@interface HouseTypeInfoViewController() <CarouselAdvertiseDelegate,UIScrollViewDelegate,HousesViewDelegate,HouseTypeInfoDelegate,UITableViewDelegate,MoreHouseTypeDelegate>
@property (nonatomic, strong) NSMutableArray *viewsArray;
@property (nonatomic, strong) HouseTypeInfoBottomBarView *bottomBarView;
@property (nonatomic, strong) MoreHouseTypeView *moreHouseTypeView;
@property (nonatomic, strong) HousesView *housesView;
@property (nonatomic, strong) UIView *commentBottomBarView;

@property (nonatomic, strong) HousesViewModel *housesViewModel;
@property (nonatomic, strong) MoreHouseTypeViewModel *moreHouseTypeViewModel;
@property (nonatomic, strong) HouseTypeInfoViewModel *houseTypeInfoViewModel;
@property (nonatomic, strong) HouseTypeInfoCommentViewModel *houseTypeInfoCommentViewModel;
@property (nonatomic, strong) CarouselAdvertiseView *carouselView;

@property (nonatomic, strong) UIView *maskView;

@end

@implementation HouseTypeInfoViewController
{
    Boolean _isMoreHouseTypeShow;
    CGFloat _moreHouseTypeHeightRatio;
    Boolean _isHousesShow;
    CGFloat _housesViewHeightRatio;
    NSInteger _selectLayoutId;
    HouseTypeInfoMessageModel *_selectMessageModel;
}
#pragma mark - Life Cycle
- (void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"户型房源";
    [self loadHouseTypeInfoViewController];
//    [self loadNetModel];
    self.view.backgroundColor = BackgroundColor240;
    self.navigationController.navigationBar.barTintColor = BackgroundColor240;
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear: animated];
    UIImageView *moreLayoutsImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
    [moreLayoutsImageView setIconWithKey:@"layoutOverAllIcon" withPlaceHolderString:DefaultNetImage1To1];
    moreLayoutsImageView.contentMode = UIViewContentModeScaleAspectFit;
    moreLayoutsImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showMoreLayoutsClick)];
    [moreLayoutsImageView addGestureRecognizer:tapGesture];
    [self.navigationItem addRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:moreLayoutsImageView] withOffset:-10];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.translucent = YES;
}

#pragma mark - Delegate CarouselAdvertiseDelegate
- (void)pageScrollToPresentView:(UIView *)presentView{
    NSInteger presentId = [_viewsArray indexOfObject:presentView];
    //使上一个view和下一个view滚动回顶部，变成初始状态
    NSInteger lastId = presentId==0?(_viewsArray.count-1):(presentId-1);
    NSInteger nextId = presentId == _viewsArray.count-1?0:(presentId+1);
    HouseTypeInfoView *lastView = [_viewsArray objectAtIndex:lastId];
    HouseTypeInfoView *nextView = [_viewsArray objectAtIndex:nextId];
    
    [lastView.houseTypeInfoTableView setContentOffset:CGPointMake(lastView.houseTypeInfoTableView.contentOffset.x, 0) animated:NO];
    [nextView.houseTypeInfoTableView setContentOffset:CGPointMake(nextView.houseTypeInfoTableView.contentOffset.x, 0) animated:NO];
   
    _selectMessageModel = _models[presentId];
    HouseTypeInfoBottomBarModel *barModel = [HouseTypeInfoBottomBarModel new];
    barModel.attentionNum = _selectMessageModel.attentionCount;
    barModel.selectedLayoutNum = presentId+1;
    barModel.totalLayoutNum = _viewsArray.count;
    barModel.totalHouseNum = _selectMessageModel.layoutHouseCount;
    self.bottomBarView.barModel = barModel;
    
    self.title = _selectMessageModel.layoutNum;
    _selectLayoutId = _selectMessageModel.layoutId;
    self.housesView.selectLayoutId = _selectLayoutId;
//    self.commentBottomBarView.alpha = 0.0;
}

#pragma mark - Delegate UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offsetY = scrollView.contentOffset.y;
    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
    if (offsetY > 80) {
        self.commentBottomBarView.alpha = (offsetY-80)*10/screenHeight;
    }else{
        self.commentBottomBarView.alpha = 0.0;
    }
    
    if (_selectMessageModel != nil) {
        
        if (_selectMessageModel.comments.count <= 1) {
            
            if (offsetY > 20) {
                self.commentBottomBarView.alpha = (offsetY-15)*15/screenHeight;
            }
        }
    }
}

#pragma mark - Delegate HousesViewDelegate
- (void)housesTableDidSelectWithHouseModel:(HousesModel *)houseModel{
//    HouseInfoViewController *houseInfoViewController = [HouseInfoViewController new];
//    houseInfoViewController.layoutId = _selectLayoutId;
//    houseInfoViewController.houseId = houseModel.houseId;
//    houseInfoViewController.projectId = [AppContext sharedInstance].projectId;
//    [self.navigationController pushViewController:houseInfoViewController animated:YES];
}


#pragma mark - Delegate HouseTypeInfoDelegate
- (void)houseTypeInfoMoreCommentClickAction{
    DDLogInfo(@"ViewController更多评论点击");
//    UniversalTableViewController *universalTableViewController = [[UniversalTableViewController alloc] init];
//    NSDictionary *postDic = @{@"pageIndex":@"0",@"pageSize":@"10"};
//    _houseTypeInfoCommentViewModel = [HouseTypeInfoCommentViewModel new];
//    _houseTypeInfoCommentViewModel.layoutId = _selectLayoutId;
//    _houseTypeInfoCommentViewModel.postObject = postDic;
//    _houseTypeInfoCommentViewModel.isShowMore = YES;
//    universalTableViewController.baseViewModel = _houseTypeInfoCommentViewModel;
//    universalTableViewController.title = @"户型评论";
//    [self.navigationController pushViewController:universalTableViewController animated:YES];
}

- (void)houseTypeInfoMediaHtmlClickActionWithUrlId:(NSInteger)urlId{
    DDLogInfo(@"有声杂志点击事件");
    YijuWebViewController *yiJuWebViewController = [[YijuWebViewController alloc] initWithHtmlId:urlId htmlTypeName:LayoutMagazine projectId:[AppContext sharedInstance].projectId Introduction:@"" andHtmlTitle:@"有声户型杂志"];
    [yiJuWebViewController loadHTMLRequest];
    [self.navigationController pushViewController:yiJuWebViewController animated:YES];
    
}

- (void)houseTypeInfoHtmlClickActionWithUrlId:(NSInteger)urlId{
    DDLogInfo(@"杂志点击事件");
    YijuWebViewController *yiJuWebViewController = [[YijuWebViewController alloc] initWithHtmlId:urlId htmlTypeName:LayoutMagazine projectId:[AppContext sharedInstance].projectId Introduction:@"" andHtmlTitle:@"户型杂志"];
    [yiJuWebViewController loadHTMLRequest];
    [self.navigationController pushViewController:yiJuWebViewController animated:YES];
}

- (void)houseTypeInfoCalculateClickActionWithHouseTotalPrice:(NSString *)totalPrice{
    DDLogInfo(@"计算器点击事件");
    HouseLoanCalculateViewController *houseLoanCalculateViewController = [HouseLoanCalculateViewController new];
    houseLoanCalculateViewController.houseTotalPrice = totalPrice;
    [self.navigationController pushViewController:houseLoanCalculateViewController animated:YES];
}

#pragma mark - Delegate MoreHouseTypeDelegate
- (void)didSelectHouseTypeWithLayoutId:(NSInteger)layoutId{
    NSInteger collectionSelectId = 0;
    for (int i=0; i<_models.count; i++) {
        
        HouseTypeInfoMessageModel *model = _models[i];
        
        if (model.layoutId == layoutId) {
                collectionSelectId = i;
            }
    }
   DDLogInfo(@"选中id:%ld,变换的id:%ld",layoutId,collectionSelectId);
    [_carouselView updateCarouselAdvertiseViewContentToCurrentViewIndex:collectionSelectId];
    [self showMoreLayoutsClick];
}

#pragma mark - Event Response
//更多户型点击事件
- (void)showMoreLayoutsClick{
    if (_isHousesShow) {
        return;
    }
    
    _isMoreHouseTypeShow = !_isMoreHouseTypeShow;
    
    if (_isMoreHouseTypeShow == YES) {
       [self.view insertSubview:self.maskView belowSubview:self.moreHouseTypeView];
        _moreHouseTypeHeightRatio = 0.75;
        [self.moreHouseTypeView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.view);
            make.height.equalTo(self.view).multipliedBy(_moreHouseTypeHeightRatio);
        }];
        [UIView animateWithDuration:0.5 animations:^{
            [self.moreHouseTypeView layoutIfNeeded];
        }];
    }else{
        _moreHouseTypeHeightRatio = 0;
        [self.maskView removeFromSuperview];
        [self.moreHouseTypeView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.view);
            make.height.equalTo(self.view).multipliedBy(_moreHouseTypeHeightRatio);
        }];
        [UIView animateWithDuration:0.5 animations:^{
            [self.moreHouseTypeView layoutIfNeeded];
        }];
    }
}
//更多房屋点击事件
- (void)houseCountTap:(UITapGestureRecognizer *)gesture{
    if (_isMoreHouseTypeShow) {
        return;
    }
    
    UILabel *housesLabel = (UILabel *)gesture.view;
    self.housesView.housesCount = housesLabel.tag;
    self.housesView.houseType =  [NSString stringWithFormat:@"%@户型",_selectMessageModel.layoutNum];
    _isHousesShow = !_isHousesShow;
    
    if (_isHousesShow == YES) {
        
        _housesViewHeightRatio = 0.7;
        [self.view insertSubview:self.maskView belowSubview:self.housesView];
        [self.housesView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.height.equalTo(self.view).multipliedBy(_housesViewHeightRatio);
            make.bottom.equalTo(self.view).offset(-ScreenHeight*0.075);
        }];
        [UIView animateWithDuration:0.5 animations:^{
            [self.housesView layoutIfNeeded];
        }];
    }else{
        _housesViewHeightRatio = 0;
        [self.maskView removeFromSuperview];
        [self.housesView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
             make.bottom.equalTo(self.view).offset(-ScreenHeight*0.075);
            make.height.equalTo(self.view).multipliedBy(_housesViewHeightRatio);
        }];
        [UIView animateWithDuration:0.5 animations:^{
            [self.housesView layoutIfNeeded];
        }];
    }
    [self.housesView loadNetModel];
}

//跳转更多评论点击
- (void)turnToCommentPage{
//    HouseTypeCommentController *houseTypeCommentController = [HouseTypeCommentController new];
//    houseTypeCommentController.houseTypeId = _selectLayoutId;
//    __weak typeof(self) weakSelf = self;
//    houseTypeCommentController.commentCommitBlock = ^{
//        [weakSelf loadNetModel];
//    };
//    [self.navigationController pushViewController:houseTypeCommentController animated:YES];
    DDLogInfo(@"hahahahha");
}

//maskView隐藏更多房屋或者户型显示
- (void)hideMoreLayoutsAndHouse{
    if (_isMoreHouseTypeShow) {
        [self showMoreLayoutsClick];
    }else{
        [self houseCountTap:self.bottomBarView.houseCountLabel.gestureRecognizers[0]];
    }
}

#pragma mark - Private
- (void)loadHouseTypeInfoViewController{
    [self.view addSubview:self.carouselView];
    [self.view addSubview:self.bottomBarView];
    [self.view addSubview:self.commentBottomBarView];
    
    [self.carouselView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.width.equalTo(self.view);
        make.height.mas_equalTo(ScreenHeight-64-ScreenHeight*0.075);
    }];
    [self.bottomBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.mas_equalTo(ScreenHeight*0.075);
    }];
    
    [self.moreHouseTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(64);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(0);
    }];
    [self.housesView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-44);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(0);
    }];
    [self.commentBottomBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.equalTo(self.bottomBarView);
    }];
    [self loadCommentBottomBarContentViewWithBackView:self.commentBottomBarView];
}

- (void)loadCommentBottomBarContentViewWithBackView:(UIView *)backView{
    UIView *barView = [UIView new];
    barView.backgroundColor = LineColor222;
    [backView addSubview:barView];
    [barView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(backView);
        make.height.mas_equalTo(1);
    }];
    UIButton *commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [commentButton setTitle:@"我要评论" forState:UIControlStateNormal];
    commentButton.layer.masksToBounds = YES;
    commentButton.layer.cornerRadius = 5;
    commentButton.backgroundColor = IndicatorColor52;
    commentButton.titleLabel.font = TextFont18;
    [commentButton addTarget:self action:@selector(turnToCommentPage) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:commentButton];

    [commentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(barView.mas_bottom);
        make.left.equalTo(backView.mas_left).offset(FifteenoffsetViewMargin);
        make.right.equalTo(backView.mas_right).offset(-FifteenoffsetViewMargin);
        make.height.equalTo(backView.mas_height).multipliedBy(0.9);
    }];
}

- (void)loadNetModel{
    NSDictionary *postDic = @{@"pageIndex":@"0",@"pageSize":@"10"};
    
    _houseTypeInfoViewModel = [HouseTypeInfoViewModel new];
    _houseTypeInfoViewModel.viewObject = self;
    _houseTypeInfoViewModel.postObject = postDic;
    
    _moreHouseTypeViewModel = [MoreHouseTypeViewModel new];
    _moreHouseTypeViewModel.viewObject = self.moreHouseTypeView;
   
    [_moreHouseTypeViewModel loadRequest];
    [_houseTypeInfoViewModel loadRequest];
    
}

#pragma mark - Getter
- (CarouselAdvertiseView *)carouselView{
    if (_carouselView == nil) {
        _carouselView = [CarouselAdvertiseView new];
        _carouselView.delegate = self;
    }
    
    return _carouselView;
}

- (HouseTypeInfoBottomBarView *)bottomBarView{
    if (_bottomBarView == nil) {
        _bottomBarView = [HouseTypeInfoBottomBarView new];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(houseCountTap:)];
        _bottomBarView.houseCountLabel.userInteractionEnabled = YES;
        [_bottomBarView.houseCountLabel addGestureRecognizer:tap];
    }
    
    return _bottomBarView;
}

- (MoreHouseTypeView *)moreHouseTypeView{
    if (_moreHouseTypeView == nil) {
        _moreHouseTypeView = [MoreHouseTypeView new];
        _moreHouseTypeView.delegate = self;
        [self.view addSubview:_moreHouseTypeView];
    }
    
    return _moreHouseTypeView;
}

- (HousesView *)housesView{
    if (_housesView == nil) {
        _housesView = [HousesView new];
        _housesView.delegate = self;
        [self.view addSubview:_housesView];
    }
    
    return _housesView;
}

- (UIView *)commentBottomBarView{
    if (_commentBottomBarView == nil) {
        _commentBottomBarView = [UIView new];
        _commentBottomBarView.alpha = 0.0;
        _commentBottomBarView.backgroundColor = [UIColor whiteColor];
    }
    
    return _commentBottomBarView;
}

- (UIView *)maskView{
    if (_maskView == nil) {
        _maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-64-ScreenHeight*0.075)];
        _maskView.backgroundColor = TextColor120;
        _maskView.alpha = 0.5;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideMoreLayoutsAndHouse)];
        [_maskView addGestureRecognizer:tapGesture];
    }
    
    return _maskView;
}

#pragma mark - Setter
- (void)setModels:(NSMutableArray *)models{
    _models = models;
    _viewsArray = [NSMutableArray array];
    
    for (HouseTypeInfoMessageModel *model in models) {
        HouseTypeInfoView *houseTypeInfoView = [HouseTypeInfoView new];
        houseTypeInfoView.houseTypeInfoMessageModel = model;
        houseTypeInfoView.layoutCommentsTableViewCellModels = model.comments;
        houseTypeInfoView.houseTypeInfoDelegate = self;
        houseTypeInfoView.houseTypeInfoTableView.delegate = self;
        [_viewsArray addObject:houseTypeInfoView];
    }
    self.carouselView.views = _viewsArray;
}

@end
